
  package com.ysd.filter;
 
 import java.io.IOException;
  
  import javax.servlet.Filter; import javax.servlet.FilterChain; import
  javax.servlet.FilterConfig; import javax.servlet.ServletException; import
  javax.servlet.ServletRequest; import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import
  javax.servlet.http.HttpServletRequest; import
  javax.servlet.http.HttpServletResponse; import
  javax.servlet.http.HttpSession;

  
@WebFilter(urlPatterns = "*.html", filterName = "LoginFilter")
  public class LoginFilter implements Filter{
  
  @Override public void destroy() { 
	  
  }
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException { // TODO
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		request.setCharacterEncoding("UTF-8");
		String uri = request.getRequestURI();
		System.out.println(uri);
		HttpSession session = request.getSession();
		Object o=session.getAttribute("user");
		if (uri.endsWith("/login.html") || uri.endsWith(".css") || uri.endsWith(".js") || uri.endsWith(".jpg")
				|| uri.endsWith(".png")) {
			chain.doFilter(request, response);
		} else if (o != null) {
			chain.doFilter(request, response);
		} else {
			response.sendRedirect("http://localhost:8080/login.html");
		}

	}
  
  @Override public void init(FilterConfig filterConfig) throws ServletException{
	  // TODO Auto-generated method stub
  
  }
  
  }
 